package com.wpml.orm;

import com.wpml.po.UcUser;
import org.junit.Test;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class OrmTest {

    @Test
    public void test01() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {


        Connection connection = JdbcUtils.getConnection();
        System.out.println(connection);

        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select id,username,phone from uc_user");

        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        System.out.println("columnCount = " + columnCount);

        Class<UcUser> userClass = UcUser.class;
        Field[] declaredFields = userClass.getDeclaredFields();
        System.out.println("declaredFields = " + declaredFields.length);

        int[] column2Property = new int[columnCount + 1];
        Arrays.fill(column2Property, -1); // 初始化数组值为 -1

        for (int i = 1; i < columnCount + 1; i++) {
            for (int j = 0; j < declaredFields.length; j++) {
                if (metaData.getColumnName(i).equalsIgnoreCase(declaredFields[j].getName())) {
                    column2Property[i] = j;
                }
            }
        }

        System.out.println(Arrays.toString(column2Property));


        if (resultSet.next()) {
            UcUser ucUser = userClass.newInstance();
            for (int i = 1; i < column2Property.length; i++) {
                if (column2Property[i] != -1) {
                    Field field = declaredFields[column2Property[i]];
                    field.setAccessible(true);
                    Class<?> type = field.getType();
                    if (type == Integer.class || type == Integer.TYPE) {
                        field.set(ucUser, resultSet.getInt(i));
                    } else if (type == String.class) {
                        field.set(ucUser, resultSet.getString(i));
                    } else if (type == Long.class || type == Long.TYPE) {
                        field.set(ucUser, resultSet.getLong(i));
                    } else if (type == Byte.class || type == Byte.TYPE) {
                        field.set(ucUser, resultSet.getByte(i));
                    } else if (type == BigDecimal.class) {
                        field.set(ucUser, resultSet.getBigDecimal(i));
                    } else if (type == Date.class) {
                        field.set(ucUser, resultSet.getDate(i));
                    } else if (type == Object.class) {
                        field.set(ucUser, resultSet.getObject(i));
                    }

                }
            }

            System.out.println(ucUser);
        }


    }


    @Test
    public void queryOneTest() throws Exception{
        String querySQL = "select id,username,phone from uc_user limit 1";
        UcUser ucUser = JdbcUtils.execQuery(querySQL, new QueryOneHandler<>(UcUser.class));
        System.out.println(ucUser);
    }

    @Test
    public void queryListTest() throws Exception{
        String querySQL = "select id,username,phone from uc_user";
        List<UcUser> ucUsers = JdbcUtils.execQuery(querySQL, new QueryListHandler<>(UcUser.class));
        System.out.println(ucUsers);
    }
}
